import { Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { LocalStorage } from '../../utils/localstorage';
import { HttpService } from "../../http/http.service";
import { Enum } from "../../utils/enum";
import { NotifyService } from "../../utils/notify.service";
import { NzCascaderOption } from "ng-zorro-antd";
import { ValidateService, ValidateType } from '../../utils/validate.service';

@Component({
  selector: 'personinfo',
  templateUrl: './personinfo.component.html',
  styleUrls: [ './personinfo.component.styl' ]
})
export class PersonInfoComponent implements OnInit {
  userInfo: any = {
    fullName: '',
    citys: [],
    phone: '',
    email: '',
    companyName: '',
    state: '',
    provinceId: '',
    cityId: '',
    areaId: '',
    company: '',
    wechatAccount: '',
    wxunionid: '',
    iswxbind: ''
  };
  @Input() parent;
  isLoading = false;
  nzOptions: NzCascaderOption[];
  @ViewChild('checkForm', { static: true }) checkForm: NgForm;
  stateList = Enum.getEnum('userType');//用户类型
  companyList = [];         // 企业一览
  genderList: any = Enum.getEnum('gender');
  yesnoList: any = Enum.getEnum('yesno');
  spanLabel: number = 7;

  @ViewChild('wxunionid', {static: true}) wxunionid: ElementRef;

  constructor(private http: HttpService, private notify: NotifyService, private validateService: ValidateService) {
  }


  ngOnInit() {

    let info: any = LocalStorage.getInfoObject();
    this.getCityList();
    this.getCompanyList();

    this.http.get("bas/user", { "userid": info.userId }).subscribe(res => {
      this.userInfo = res.data;
      this.userInfo.citys = [];
      this.userInfo.citys.push(this.userInfo.provinceId);
      this.userInfo.citys.push(this.userInfo.cityId);
      this.userInfo.citys.push(this.userInfo.areaId);
      // 微信是否绑定
      if (res.data.wxunionid) {
        this.userInfo.iswxbind = '是';
      } else {
        this.userInfo.iswxbind = '否';
      }
    });
  }

  //校验定义
  validateDef: any = {
    fullName: {
      validator: [ { type: ValidateType.required, value: true }, {
        type: ValidateType.maxLength,
        value: 20
      } ]
    },
    gender: { validator: [ { type: ValidateType.required, value: true } ] },
    email: {
      validator: [ { type: ValidateType.email, value: true }, { type: ValidateType.maxLength, value: 100 } ]
    },
    qq: { validator: [ { type: ValidateType.qq, value: true }, { type: ValidateType.maxLength, value: 20 } ] }
  };


  onClose(): void {
    this.parent.currUserName=this.userInfo.fullName;
    this.parent.openPerson.close();
  }

  // 获取省市县一览
  getCityList() {
    this.http.get('/bas/city/tree', '').subscribe(res => {
      if (res.err == '0') {
        this.nzOptions = res.data;
      } else {
        this.notify.error(res.errMsg).subscribe();
      }
    });
  }

  // 获取企业一览
  getCompanyList() {
    let para = {
      page: 1,
      limit: 0,
      params: encodeURI(JSON.stringify({
        orderBy: 'CONVERT(m.name USING gbk)' // 排序字段
      }))
    };
    this.http.get('/evl/company', para).subscribe(res => {
      if (res.err == '0') {
        this.companyList = res.data;
      } else {
        this.notify.error(res.errMsg).subscribe();
      }
    });
  }

  onSave(): void {
    let ret: boolean = this.validateService.validate(this.checkForm, this.validateDef);

    if (!ret) return;

    // 2020/04/21 王志朋追加
    // 二维码扫描后由外部JS修改画面wxunionid的值，此时userInfo中的相应值没有随着改变
    this.userInfo.wxunionid = this.wxunionid.nativeElement.value;

    this.isLoading = true;
    this.http.put("bas/user", null, this.userInfo).subscribe(res => {
      this.isLoading = false;
      if (res.err == '0') {
        this.notify.successMsg("001010").subscribe(res => {
          this.onClose();
        });

      } else {
        this.notify.error(res.errMsg).subscribe();
      }
    });
  }


}
